﻿//-----------------------------------------------------------------------
// <copyright file="Locks.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System.Threading;

#endregion

/*
 * SOURCES:
 * http://devplanet.com/blogs/brianr/archive/2008/09/26/thread-safe-dictionary-in-net.aspx
 */

namespace GateShift.Services.SDK.Threading
{
    /// <summary>
    /// General locking class to manage ReaderWriterLockSlim
    /// </summary>
    public static class Locks
    {
        #region Get Locks

        /// <summary>
        /// Creates a read lock on the object
        /// </summary>
        /// <param name="locks">ReaderWriterLockSlim to create the lock on</param>
        public static void GetReadLock(ReaderWriterLockSlim locks)
        {
            while (!locks.TryEnterUpgradeableReadLock(1))
            {
            }
        }

        /// <summary>
        /// Creates a read only lock on the object
        /// </summary>
        /// <param name="locks">ReaderWriterLockSlim to create the lock on</param>
        public static void GetReadOnlyLock(ReaderWriterLockSlim locks)
        {
            while (!locks.TryEnterReadLock(1))
            {
            }
        }

        /// <summary>
        /// Creates a write lock on the object
        /// </summary>
        /// <param name="locks">ReaderWriterLockSlim to create the lock on</param>
        public static void GetWriteLock(ReaderWriterLockSlim locks)
        {
            while (!locks.TryEnterWriteLock(1))
            {
            }
        }

        #endregion

        #region Release Locks

        /// <summary>
        /// Release any current read lock on the object
        /// </summary>
        /// <param name="locks">ReaderWriterLockSlim to release the lock on</param>
        public static void ReleaseReadLock(ReaderWriterLockSlim locks)
        {
            if (locks.IsUpgradeableReadLockHeld)
            {
                locks.ExitUpgradeableReadLock();
            }
        }

        /// <summary>
        /// Release any current read only lock on the object
        /// </summary>
        /// <param name="locks">ReaderWriterLockSlim to release the lock on</param>
        public static void ReleaseReadOnlyLock(ReaderWriterLockSlim locks)
        {
            if (locks.IsReadLockHeld)
            {
                locks.ExitReadLock();
            }
        }

        /// <summary>
        /// Release any current write lock on the object
        /// </summary>
        /// <param name="locks">ReaderWriterLockSlim to release the lock on</param>
        public static void ReleaseWriteLock(ReaderWriterLockSlim locks)
        {
            if (locks.IsWriteLockHeld)
            {
                locks.ExitWriteLock();
            }
        }

        /// <summary>
        /// Releases all current locks on the locking system
        /// </summary>
        /// <param name="locks">ReaderWriterLockSlim to release the locks on</param>
        public static void ReleaseAllLocks(ReaderWriterLockSlim locks)
        {
            ReleaseWriteLock(locks);
            ReleaseReadLock(locks);
            ReleaseReadOnlyLock(locks);
        }

        #endregion

        #region Instancing

        /// <summary>
        /// Creates a new lock object with the default policy
        /// </summary>
        /// <returns>ReaderWriterLockSlim for use in creating locks</returns>
        public static ReaderWriterLockSlim GetLockInstance()
        {
            return GetLockInstance(LockRecursionPolicy.SupportsRecursion);
        }

        /// <summary>
        /// Creates a new lock object with the specified policy
        /// </summary>
        /// <param name="recursionPolicy">LockRecursionPolicy to follow</param>
        /// <returns>ReaderWriterLockSlim for use in creating locks</returns>
        public static ReaderWriterLockSlim GetLockInstance(LockRecursionPolicy recursionPolicy)
        {
            return new ReaderWriterLockSlim(recursionPolicy);
        }

        #endregion
    }
}